package cn.itcast.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class ES_Client {


    public static void main(String[] args) throws IOException {
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.20.129", 9200, "http"))
        );

//        // 创建索引
//        CreateIndexRequest request = new CreateIndexRequest("test_index");
//        CreateIndexResponse testIndex = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
//        // 获取响应
//        System.out.println("创建索引:"+testIndex.isAcknowledged());

        //查询索引
        GetIndexRequest test_index = new GetIndexRequest("test_index");
        GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(test_index, RequestOptions.DEFAULT);
        // 获取响应
        System.out.println("查询索引别名:"+getIndexResponse.getAliases());
        System.out.println("查询索引设置:"+getIndexResponse.getSettings());
        System.out.println("查询索引映射:"+getIndexResponse.getMappings());

        // 删除索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test_index");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        // 获取响应
        System.out.println("删除索引:"+delete.isAcknowledged());


        // 关闭客户端
        restHighLevelClient.close();
    }
}
